//
//  HMVisitorView.swift
//  Weibo
//
//  Created by fuyu on 2017/7/23.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit

import SnapKit
//APPKey:2379901287  App Secret：debadaabc2fff65bb121b81de7f9b25f


//  访客视图代理协议
protocol HMVisitorViewDelegate: NSObjectProtocol {
    //  登录或者注册的操作
    func loginOperation()
}

//访客视图
class HMVisitorView: UIView {
    //点击登录或者注册需要执行的闭包
    var loginCallBack: (() -> ())?
    //  接收代理对象, 代理对象需要一般会使用弱引用
    weak var delegate: HMVisitorViewDelegate?
    
    //懒加载控件
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //罩层
    private lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //icon
    private lazy var homeImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息内容
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.black
        label.textAlignment = .center
        //多行显示
        label.numberOfLines = 0
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: #selector(registerButtonAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        //  设置不同状态下文字的颜色
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        //图片拉伸
        let edgeInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        //  图片拉伸拉伸方式2
        let image = UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: edgeInset)
        //设置背景图片
        button.setBackgroundImage(image, for: .normal)
        //  取消点击高亮
        button.adjustsImageWhenHighlighted = false
        
        return button
    }()
    
    //  登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: #selector(loginButtonAction), for: .touchUpInside)
        button.setTitle("登陆", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        //  设置不同状态下文字的颜色
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        //图片拉伸
        let edgeInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        //  图片拉伸拉伸方式2
        let image = UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: edgeInset)
        //设置背景图片
        button.setBackgroundImage(image, for: .normal)
        //  取消点击高亮
        button.adjustsImageWhenHighlighted = false
        
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //禁用xib或sb加载,会提示错误
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237.0 / 255, alpha: 1.0)
        //添加子控件
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //添加约束
        cycleImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskIconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        homeImageView.snp.makeConstraints { (make) in
            make.center.equalTo(maskIconImageView)
        }
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cycleImageView.snp.bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        registerButton.snp.makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        loginButton.snp.makeConstraints { (make) in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
    }
    
    //开启旋转动画
    private func startAction() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  动画目的地
        animation.toValue = 2 * Double.pi
        // 动画时长
        animation.duration = 20
        //执行次数
        animation.repeatCount = MAXFLOAT
        //在执行次数内失去焦点不释放动画对象
        animation.isRemovedOnCompletion = false
        //执行动画
        cycleImageView.layer.add(animation, forKey: nil)
        
    }
    
//    //  修改访客视图信息
    func updateVisitorInfo(message: String?, imageName: String?) {
        
        if let msg = message, let imgName = imageName {
            //  不是首页
            //  隐藏旋转图片
            cycleImageView.isHidden = true
            messageLabel.text = msg
            homeImageView.image = UIImage(named: imgName)
        }else {
            //是首页
            startAction()
        }
    }
    
    //  MARK: --    点击登录和注册执行的方法
    @objc private func registerButtonAction() {
        
        //执行闭包,完成回调
        loginCallBack?()
        //  使用代理对象调用代理方法完成回调
        delegate?.loginOperation()
    }
    @objc private func loginButtonAction() {
        loginCallBack?()
        //  使用代理对象调用代理方法完成回调
        delegate?.loginOperation()
    }
    
    
    
}
